package com.hangzhou.xyj.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.UserDao;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.manager.ChatManager;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.net.handler.LoginHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.LoginReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.utils.constants.DataKey;

import cn.jpush.android.api.JPushInterface;

/**
 * 启动界面
 *
 * @author neo.duan
 */
public class SplashActivity extends BaseActivity {
    private boolean isFromNotifictaion;
    private Bundle extraBundle;

    @Override
    public void initIntentData(Intent intent) {
        isFromNotifictaion = intent.getBooleanExtra(DataKey.IS_FROM_NOTIFICATION, false);
        if (isFromNotifictaion) {
            extraBundle = new Bundle();
            extraBundle.putBoolean(DataKey.IS_FROM_NOTIFICATION, isFromNotifictaion);
            extraBundle.putString(DataKey.URL, intent.getStringExtra(DataKey.URL));
            extraBundle.putString(DataKey.MESSAGE, intent.getStringExtra(DataKey.MESSAGE));
            extraBundle.putInt(DataKey.PUSH_TYPE, intent.getIntExtra(DataKey.PUSH_TYPE, -1));
        }
    }

    @Override
    public void initTop() {
        enableTop(false);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    public void initViews() {
        MyImageView imageView = findView(R.id.image);
        imageView.displayImage("drawable://" + R.drawable.bg_splash);
    }

    @Override
    public void initListeners() {

    }


    @Override
    public void initDatas() {

    }

    /**
     * 延迟进入主页
     */
    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
        UserDao dao = new UserDao(mContext);
        UserModel user = dao.getUser(CommonPreferences.getUserId(mContext));
        if (user == null) {
            goNextPage();
            return;
        }

        LoginReq req = new LoginReq(user.getName(), user.getPassword());
        HttpLoader.getInstance().sendRequest(new LoginHandler(req, new MyHttpResponseListener() {

            @Override
            public void onStart() {
                //不要loading框
            }

            @Override
            public void onSuccess(Object model) {
//                ToastUtil.show("登录成功...");
                //登录成功，保存用户信息到数据库
                UserDao dao = new UserDao(mContext);
                dao.addUser((UserModel) model);
                //保存userId到配置中，用于标识读取数据库信息
                String userId = ((UserModel) model).getUserId() + "";
                CommonPreferences.saveUserId(mContext, userId);
                //保存一份到应用中
                XYJApplication.getInstance().setUserInfo((UserModel) model);

                //登录环信
                ChatManager.getInstance().login(userId, "123456");

                goNextPage();
            }

            @Override
            public void onFail(String msg) {
                //登录失败：可能用户名或密码有错，清除本地账户
                UserDao dao = new UserDao(mContext);
                dao.clear();
                CommonPreferences.saveUserId(mContext, "-1");
                goNextPage();
            }

        }, 0));
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    private void goNextPage() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i;
                if (CommonPreferences.isShowGuideView(mContext)) { //不是第一次使用，则直接跳转到应用的主页面
                    if (extraBundle != null) {
                        i = new Intent(mContext, MainActivity.class);
                        i.putExtra(DataKey.BUNDLE, extraBundle);
                        mContext.startActivity(i);
                    } else {
                        IntentManager.getInstance().goMainActivity(mContext);
                    }
                } else {
                    if (extraBundle != null) {
                        i = new Intent(mContext, GuideActivity.class);
                        i.putExtra(DataKey.BUNDLE, extraBundle);
                        mContext.startActivity(i);
                    } else {
                        IntentManager.getInstance().goGuideActivity(mContext);
                    }
                }
                closeActivity();
            }
        }, 2000);
    }
}
